namespace VRage.Utils
{
	/// <summary>
	/// Item that is accessible only for defined amount of time ticks.
	/// </summary>
	public struct MyTickTimedItemF
	{
		private float m_storage;

		private int m_ticksLeft;

		/// <summary>
		/// Get the stored item.
		/// </summary>
		/// <returns>storage on success, default value of T on failure.</returns>
		public float Get()
		{
			if (m_ticksLeft <= 0)
			{
				return 0f;
			}
			m_ticksLeft--;
			return m_storage;
		}

		/// <summary>
		/// Get the stored item.
		/// </summary>
		/// <param name="outStoredItem">item stored internally</param>
		/// <returns>true on success, false on timeout</returns>
		public bool TryGet(out float outStoredItem)
		{
			if (m_ticksLeft > 0)
			{
				outStoredItem = m_storage;
				m_ticksLeft--;
				return true;
			}
			outStoredItem = 0f;
			return false;
		}

		/// <summary>
		/// Set the stored value.
		/// </summary>
		/// <param name="itemTickTimeout">Number of time ticks for which the item is accessible.</param>
		/// <param name="item">Item to be stored.</param>
		public void Set(int itemTickTimeout, float item)
		{
			m_storage = item;
			m_ticksLeft = itemTickTimeout;
		}
	}
}
